<template>
  <div class="menu-management-content">
    <div class="query-content">
      <el-form
        :inline="true"
        :model="formInline"
        class="demo-form-inline backend-form"
      >
        <el-form-item label="菜单名称">
          <el-input v-model="formInline.name" @change="onSubmit" />
        </el-form-item>
        <!-- <el-form-item label="菜单状态">
          <el-select v-model="formInline.menuState" placeholder="请选择" clearable>
            <el-option
              v-for="(item, index) in stateDict"
              :key="index"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item> -->
        <!--        <el-form-item>-->
        <!--          <el-button type="primary" class="queryButton" @click="onSubmit"-->
        <!--            >查询</el-button-->
        <!--          >-->
        <!--          <el-button type="primary" class="resetButton" @click="onReset"-->
        <!--            >重置</el-button-->
        <!--          >-->
        <!--        </el-form-item>-->
        <el-form-item class="backend-button">
          <el-button
            class="addButton"
            type="primary"
            @click="handleAddClick"
            v-if="currentUser?.hasPermission('cdgladd')"
          >
            <!--        <el-icon>-->
            <!--          <Plus />-->
            <!--        </el-icon>-->
            <i class="icon el-icon-plus"></i>
            {{ currentUser?.getButtonName('cdgladd') }}
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-content">
      <!--      <el-button class="addButton" type="primary" @click="handleAddClick">-->
      <!--        &lt;!&ndash;        <el-icon>&ndash;&gt;-->
      <!--        &lt;!&ndash;          <Plus />&ndash;&gt;-->
      <!--        &lt;!&ndash;        </el-icon>&ndash;&gt;-->
      <!--        <i class="icon el-icon-plus"></i>-->
      <!--        新增-->
      <!--      </el-button>-->
      <div class="table">
        <el-table :data="tableData" stripe style="width: 100%" row-key="pkid">
          <el-table-column
            label="菜单名称"
            align="left"
            width="180"
            prop="catalogName"
          />
          <el-table-column
            prop="menuType"
            label="类型"
            show-overflow-tooltip
          >
            <template #default="scope">
              <span v-if="scope.row.menuType === '0'">菜单</span>
              <span v-else>按钮</span>
            </template>
          </el-table-column>
          <!--          <el-table-column prop="icon" label="图标">-->
          <!--            <template #default="scope">-->
          <!--              <div style="width: 24px">-->
          <!--                <img-->
          <!--                  :src="scope.row.unChoosePicture"-->
          <!--                  v-if="scope.row.unChoosePicture"-->
          <!--                  class="icon"-->
          <!--                />-->
          <!--                <Icon-->
          <!--                  :data="scope.row.icon"-->
          <!--                  v-else-if="scope.row.icon"-->
          <!--                  :size="24"-->
          <!--                ></Icon>-->
          <!--              </div>-->
          <!--            </template>-->
          <!--          </el-table-column>-->
          <el-table-column
            label="顺序"
            prop="orderIndex"
            show-overflow-tooltip
          />
          <!-- <el-table-column prop="path" label="路由地址" /> -->
          <el-table-column prop="menuState" label="状态" show-overflow-tooltip>
            <template #default="scope">
              <el-button type="text" text link v-if="scope.row.status === 1"
                >启用</el-button
              >
              <span v-else style="color: #ff455f">禁用</span>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template #default="scope">
              <el-button
                text
                type="text"
                style="padding: 0"
                @click="handleEditClick(scope.row)"
                v-if="currentUser?.hasPermission('cdglupdate')"
              >
                {{ currentUser?.getButtonName('cdglupdate') }}
              </el-button>
              <el-popconfirm
                width="160px"
                confirm-button-text="确认"
                cancel-button-text="取消"
                icon-color="#626AEF"
                @confirm="handleDelete"
                :title="'确认删除' + scope.row.catalogName + '吗？'"
              >
                <template #reference>
                  <el-button
                    text
                    type="text"
                    style="color: #fa3434; margin-left: 12px"
                    @click="handleDeleteClick(scope.row)"
                    v-if="currentUser?.hasPermission('cdgldelete')"
                  >
                    {{ currentUser?.getButtonName('cdgldelete') }}
                  </el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- <el-pagination
        background
        layout="slot, prev, pager, next"
        :page-size="formInline.pageSize"
        :current-page="formInline.pageIndex"
        :total="total"
        @current-change="handlePageChange"
      >
        <template #default> 共{{ total }}个菜单 </template>
      </el-pagination> -->
    </div>
    <add-menu-dialog
      :menu-info="menuInfo"
      :menuOptions="menuOptions"
      :visible="dialogVisible"
      :parent-menu-dict="stateDict"
      @closeDialog="handleDialogClose"
      @updateMenu="handleUpdateMenu"
    ></add-menu-dialog>
    <!--    <el-dialog-->
    <!--      :visible.sync="deleteVisible"-->
    <!--      title="提示"-->
    <!--      align-center-->
    <!--      class="outline-dialog"-->
    <!--      width="30%"-->
    <!--    >-->
    <!--      <span>确认删除“{{ menuInfo.catalogName }}”吗？</span>-->
    <!--      <template #footer>-->
    <!--        <div class="dialog-footer">-->
    <!--          <el-button @click="deleteVisible = false">取消</el-button>-->
    <!--          <el-button type="primary" @click="handleDelete"> 确认 </el-button>-->
    <!--        </div>-->
    <!--      </template>-->
    <!--    </el-dialog>-->
  </div>
</template>

<script setup lang="ts">
import { getCurrentInstance, onMounted, reactive, ref, computed } from 'vue'
import AddMenuDialog from '@/views/backend-management/components/menu/components/addMenuDialog.vue'
// import { Plus } from '@element-plus/icons-vue'
import type { OptionData } from '@/utils/type'
import { queryMenuList } from '@/api/back-management/menu'
import type { MenuTree } from '@/api/back-management/menu/interface'
import { deleteMenuTree } from '@/api/back-management/menu'
// import { useCurrentUserStore } from '@/stores/current-user'
import { PERMISSIONS } from '@/common/user'
// import { Icon } from 'vue3-icon-picker'
import { Message as ElMessage } from 'element-ui'

const vm = getCurrentInstance()
const store = vm?.proxy?.$store

const currentUser = store?.state?.user?.currentUser

// const { currentUser } = useCurrentUserStore()

const stateDict = ref<OptionData[]>([])

const formInline = reactive({
  name: '',
  type: '4',
})

const dialogVisible = ref(false)

const tableData = ref<MenuTree[]>([])
const menuInfo = reactive({
  pkid: '',
  catalogName: '',
  status: 1
})

const menuOptions = ref<MenuTree[]>([])

const deleteVisible = ref(false)

const onSubmit = () => {
  getMenuList()
}

// const addFieldToChild = (child: MenuTree) => {
//   if (child.children.length) {
//     child.hasChildren = true;
//     child.children.map((val) => {
//       addFieldToChild(val);
//     });
//   } else {
//     child.hasChildren = false;
//   }
// };

const getMenuList = () => {
  queryMenuList(formInline)
    .then((res) => {
      if (res.success) {
        tableData.value = res.data[0].children
        if (!formInline.name) {
          menuOptions.value = res.data
        }
        // tableData.value = res.data[0].children.map((child) => {
        //   addFieldToChild(child);
        //   return child;
        // });
        // console.log(tableData);
      } else {
        ElMessage.error(res.message)
      }
    })
    .catch((e) => {
      ElMessage.error(e)
    })
}

const onReset = () => {
  formInline.name = ''
  getMenuList()
}

const handleEditClick = (item: any) => {
  Object.assign(menuInfo, item)
  dialogVisible.value = true
}

const handleAddClick = () => {
  Object.assign(menuInfo, {})
  dialogVisible.value = true
}

const handleDeleteClick = (item: any) => {
  Object.assign(menuInfo, item)
  deleteVisible.value = true
}

const handleDialogClose = () => {
  dialogVisible.value = false
  Object.assign(menuInfo, {
    menuType: '0',
    parentId: '',
    catalogName: '',
    icon: '',
    orderIndex: 0,
    status: 1,
    pkid: '',
    address: '',
    choosePicture: '',
    chooseSvgPicture: '',
    showStatus: 1,
    routePath: '',
    code: '',
  })
}

const handleUpdateMenu = () => {
  dialogVisible.value = false
  getMenuList()
}

const handleDelete = () => {
  deleteMenuTree(menuInfo.pkid)
    .then((res) => {
      if (res.success) {
        onSubmit()
        ElMessage.success('删除菜单成功！')
      } else {
        ElMessage.error(res.message)
      }
    })
    .catch((e) => {
      ElMessage.error(e)
    })
    .finally(() => {
      deleteVisible.value = false
    })
}

onMounted(() => {
  getMenuList()
})
</script>

<style lang="scss" scoped>
.menu-management-content {
  width: 100%;
  height: calc(100% - 27px);
  background-color: white;
  box-shadow: 0 0 6px 0 rgba(0, 0, 0, 0.1);
  border-radius: 2px;

  .query-content {
    box-sizing: border-box;
    padding-left: 20px;
    width: 100%;
    height: 80px;
    line-height: 80px;
    border-bottom: 1px solid #eeeff5;

    :deep(.el-form) {
      .el-form-item {
        margin-bottom: 0;

        .resetButton {
          width: 80px;
          height: 32px;
          color: #28b1e4;
          background: #ffffff;
          border: 1px solid #28b1e4;
          border-radius: 2px;
        }

        .queryButton {
          width: 80px;
          height: 32px;
          background: #28b1e4;
          border-radius: 2px;
        }
      }
    }
  }
  .addButton {
    margin-left: 20px;
    margin-top: 14px;
    margin-bottom: 14px;
    width: 100px;
    height: 32px;
    line-height: 32px !important;
    //background: #28b1e4;
    border-radius: 2px;
    //border: none;
    padding: 0;
  }
  .table-content {
    width: 100%;
    height: calc(100% - 63px);

    .table {
      box-sizing: border-box;
      width: 100%;
      padding: 0 18px;
      height: calc(100% - 77px - 32px - 28px);
      overflow-y: auto;

      .userName {
        .item {
          margin-left: 6px;
        }

        :deep(.el-icon) {
          vertical-align: sub;
          .svg-icon {
            svg {
              width: 16px;
              height: 16px;
              color: #28b1e4;
            }
          }
        }

        :deep(.el-badge__content) {
          width: 12px;
          height: 13px;
          transform: translateY(85%) translateX(148%);
        }
      }

      :deep(.el-table__row--striped) {
        background: #f7f9fb;
      }
    }

    .el-pagination {
      margin-top: 15px;
      justify-content: end;
      margin-right: 18px;
    }
  }

  .icon {
    width: 22px;
    height: 22px;
  }
}
</style>
